import { isEmpty } from 'shiptools'
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        center: {
            type: Object,
            value: () => {}
        },
        spotsList: {
            type: Array,
            value: () => [],
            observer: function (newVal, oldVal) { 
              if(newVal !== oldVal && !isEmpty(newVal)) {
                this.init(newVal)
              }
            }
        }
    },
  
    /**
     * 组件的初始数据
     */
    data: {
        markers: []
    },
  
    /**
     * 组件的方法列表
     */
    methods: {
        onMarkerTapHandle: function(e) {
            const markerId = e.detail.markerId
            this.triggerEvent('marker', markerId)
        },
        onCallOutTapHandle: function(e) {
            const markerId = e.detail.markerId
            this.triggerEvent('label', markerId)
        },
        onTap: function(e) {
            const markerId = e.detail.markerId
            this.triggerEvent('center', markerId)
        },

        init: function(data) {
            const markers = this.markerBuilder(data)
            this.setData({
                markers
            })
        },

        markerBuilder(spotsList) {
            const _markers = []
            try {
                spotsList.forEach(item => {
                    _markers.push({
                        id: item.id,
                        latitude: item.latitude,
                        longitude: item.longitude,
                        width:17,
                        height: 17,
                        iconPath:'https://file.40017.cn/ship/ship/images/baiduapp/secondary-icon.png',
                        callout:{
                            content: item.name,
                            color:'#ff6257',
                            bgColor:'#ffffff',
                            padding:10,
                            borderRadius: 50,
                            textAlign:'center',
                            display:'ALWAYS'
                        }
                    })
                })
            } catch(err) {
                console.log(err)
            }

            return _markers
        },
    }
  })
  